package com.zxy.employee.controller;


import cn.hutool.http.HttpStatus;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zxy.base.dto.PageRequest;
import com.zxy.base.dto.Result;
import com.zxy.base.util.ResultUtil;
import com.zxy.base.util.ValidateUtil;
import com.zxy.employee.dto.EmployeeDto;
import com.zxy.employee.entity.EmployeeEntity;
import com.zxy.employee.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;


@RestController
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @PostMapping("/findEmployeePageByNameAndStatus")
    public Result  findEmployeePageByNameAndStatus(@RequestBody PageRequest<EmployeeDto> pageRequest) {
        Page<EmployeeDto> employeeByNameAndStatus = employeeService.findEmployeeByNameAndStatus(pageRequest.getPageNum(),
                pageRequest.getSize(), pageRequest.getRequest().getEmployeeName(),
                pageRequest.getRequest().getEmployStatus());

        return ResultUtil.success(employeeByNameAndStatus);
    }

    @GetMapping("/getEmployeeById/{id}")
    public Result<EmployeeDto> getEmployeeById(@PathVariable("id")Integer id) {
        EmployeeDto employeeById = employeeService.findEmployeeById(id);
        return ResultUtil.success(employeeById);
    }

    @PostMapping("/insertEmployee")
    public Result insertEmployee(@Valid @RequestBody EmployeeEntity employeeEntity) {
        ValidateUtil.validateIdCard(employeeEntity.getIdCard());
        ValidateUtil.validatePhone(employeeEntity.getPhone());
        EmployeeEntity employeeEntityResult = employeeService.insertEmployee(employeeEntity);
        return ResultUtil.success(employeeEntityResult);
    }

    @PostMapping("/updateEmployee")
    public Result updateEmployee(@RequestBody EmployeeEntity employeeEntity){
        boolean updateResult = employeeService.updateEmployee(employeeEntity);
        return ResultUtil.success(updateResult);
    }

    @PostMapping("/importEmployee")
    public Result importEmployee(MultipartFile file) throws IOException {
        String s = employeeService.insertEmployeeFromUploadFile(file);

        return new Result(HttpStatus.HTTP_OK,"errlog filename",s);
    }








}
